var linkageSelectMixin = function (dropdownField) {
  if (dropdownField === undefined) {
    dropdownField = 'options';
  }

  return {
    data: function () {
      return {
        vm: this
      }
    },

    watch:{
      changeData:function(newData, oldData){
        var fields = this.fields;
        var initField = null;
        for (var i = 0; i < fields.length; i++) {
          var fieldObj = fields[i];
          var field = fieldObj.field;
          if (newData[field] !== oldData[field]) {
            if (initField === null) {
              initField = field;
            } else {
              initField = undefined;
            }
          }
        }
        if (initField !== null) {
          this.initDropdown(initField);
        }
      }
    },

    computed: {
      changeData:function() {
        var search = this.search;
        var fields = this.fields;
        var obj = {};
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = search[field.field];
          obj[field.field] = value;
        }
        return obj;
      }
    },

    methods: {
      setDropdownData: function (vueData, fields) {
        var searchFields = [];
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var obj;
          if (typeof field !== 'string') {
            obj = field;
          } else {
            obj = {
              field
            };
          }
          vueData[`${obj.field}List`] = undefined;
          searchFields.push(obj);
        }
        vueData.fields = searchFields;
      },

      initDropdown(changeField) {
        var fields = this.fields;
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i].field;
          if (field === changeField) {
            continue;
          }
          this[`${field}List`] = undefined;
        }
      },

      setFilterDropdown: function (item) {
        var field = item.field;
        var list = this[`${field}List`];
        if (list !== undefined) {
          return;
        }

        var dropdownList = this.getFilterDropdown(item);
        this[`${field}List`] = dropdownList;
      },

      getFilterDropdown: function (item) {
        var fields = this.fields;
        var search = this.search;
        var filterObj = {};
        for (var i = 0; i < fields.length; i++) {
          var fieldObj = fields[i];
          if (fieldObj.field === item.field) {
            continue;
          }

          var searchField = fieldObj.valueField ? fieldObj.valueField : fieldObj.field;
          if (fieldObj.valueDataField) {
            searchField = fieldObj.valueDataField;
          }
          var value = _.get(search, searchField, '');
          if (typeof value === 'string') {
            if (value.trim() === '') {
              continue;
            }
          } else {
            if (SysUtil.isEmpty(value)) {
              continue;
            }
          }

          var field = fieldObj.valueField ? fieldObj.valueField : fieldObj.field;
          filterObj[field] = value;
        }

        var dropdownList = this[dropdownField];
        if (!SysUtil.isEmptyObjct(filterObj)) {
          dropdownList = _.filter(dropdownList, filterObj);
        }

        if (item.valueField === undefined) {
          var field = item.field;
          dropdownList = _.map(dropdownList, field);
          dropdownList = _.uniq(dropdownList);
          dropdownList = _.filter(dropdownList, function (value) {
            return !SysUtil.isEmpty(value)
          });
        } else {
          var labelField = item.labelField;
          if (!_.isArray(labelField)) {
            labelField = [labelField];
          }

          var valueField = item.valueField;
          var searchFields = [];
          if (labelField.indexOf(valueField) === -1) {
            searchFields = [valueField];
            searchFields.pushArray(labelField);
          } else {
            searchFields = _.clone(labelField);
          }
          dropdownList = _.map(dropdownList, function (item) {
            var obj = {};
            for (var i = 0; i < searchFields.length; i++) {
              var searchField = searchFields[i];
              var value = item[searchField];
              if (SysUtil.isEmpty(value)) {
                value = '';
              }
              obj[searchField] = value;
            }
            return obj;
          });
          dropdownList = _.uniqBy(dropdownList, valueField);
        }
        dropdownList = _.clone(dropdownList);
        return dropdownList;
      },

      visibleChange: function (visible, item) {
        if (!visible) {
          return;
        }

        this.setFilterDropdown(item);
      }
    }
  };
};
